package com.xzakota.minesweeper.ui.dialog;

import com.xzakota.minesweeper.controller.Game;
import com.xzakota.minesweeper.controller.GameRecords;
import com.xzakota.minesweeper.model.entity.LevelBean;
import com.xzakota.minesweeper.model.entity.RecordBean;
import com.xzakota.minesweeper.ui.dialog.templae.AbstractBaseDialog;

import javax.swing.*;
import java.awt.*;

/**
 * 扫雷英雄榜对话框
 *
 * @author Xzakota
 */
public class HeroListDialog extends AbstractBaseDialog {
    private final JPanel recordsPanel;

    public HeroListDialog() {
        setTitle("扫雷英雄榜");

        recordsPanel = new JPanel();
        recordsPanel.setLayout(new GridBagLayout());
        setComponent(recordsPanel, BorderLayout.NORTH);

        setConfirmButton("重新计分");
        setConfirmAction(e -> {
            Game.dispatchEvent("resetRecords");
        });

        style1Buttons();
    }

    public void updateView(GameRecords record) {
        recordsPanel.removeAll();
        showRecord(record.basicLevelRecord, 1);
        showRecord(record.intermediateLevelRecord, 2);
        showRecord(record.expenseLevelRecord, 3);
        this.pack();
    }

    private void showRecord(RecordBean record, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        Insets insets = new Insets(0, 0, 0, 0);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = insets;
        gbc.ipadx = 0;
        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.weightx = 0;
        insets.left = 0;
        LevelBean level = LevelBean.getGameLevelByID(record.levelID);
        this.createLabelCell(recordsPanel, level.name + ":", gbc);
        insets.left = 12;
        gbc.weightx = 0.5;
        gbc.gridx = 2;
        this.createLabelCell(recordsPanel, record.playTime + " 秒", gbc);
        gbc.gridx = 3;
        gbc.weightx = 0.5;
        insets.left = 50;
        gbc.ipadx = 80;
        this.createLabelCell(recordsPanel, record.player, gbc);
    }

    private void createLabelCell(JPanel panel, String txt, GridBagConstraints gbc) {
        JLabel lbl = new JLabel(txt);
        lbl.setFont(new Font("宋体", Font.PLAIN, 12));
        panel.add(lbl, gbc);
    }
}
